import sys
from Map import Map
from communication import Communication
from Entity import *


class Main (object) :


    if __name__ == "__main__" : 
        rows=0
        cols=0
        comm = Communication("localhost", 8080)
        
        """gameOver=0"""
        #Envoi du pseudo (plus tard il sera en argument)        
        comm.send("nick toto")
        
        #Recuperation des donnees initiales
        initialData = comm.receive()
        print "initial Data : " , initialData
        initialCommands = initialData.split("\n")
        print "initialCommands : " , initialCommands
        
        for index in range(len(initialCommands)) :
            line = initialCommands[index].split(" ")
            if line[0] == "rows" :
                rows = int(float(line[1]))
            
            elif line[0] =="cols" :
                cols = int(float(line[1]))
                
        #Initialisation de la carte
        map = Map(rows,cols)
        
        #Affichage de la map        
        for i in xrange (-1,rows+1) :
            for j in xrange (-1, cols+1) :
                if (map.getSurface(i,j) == 'W') :
                    sys.stdout.write(map.getSurface(i,j))
                else :
                    sys.stdout.write(map.getEntityString(i,j))
                sys.stdout.flush()
            print
        
        #Tours 
        while """gameOver==0""":
            comm.send("end")  
            data = comm.receive()
            # interpretation des messages recus
            commands = data.split("\n")
            print "Commandes : " , commands

            if commands[0] == "end" :
                gameOver = 1
                print ("Game Over")
                break

            for index in range(len(commands)) :
                line = commands[index].split(" ")
    
                if line[0] == "water" :
                    map.setSurface("WATER",int(float(line[1])),int(float(line[2])) )
        
                elif line[0] == "zombie" : 
                    ent = Zombie(int(line[1]))
                    map.setEntity(ent, int(line[2]), int(line[3]))
                    print"okkkk"
        
                elif line[0] == "entity" :
                    #HUMAN
                     if int(line[3]) == 0 :
                         ent = Human()
                         map.setEntity(ent, int(line[1]), int(line[2]))
                     #COP
                     if int(line[3])== 1 :
                         ent = Human()
                         ent.setBulletAmount(2)
                         map.setEntity(ent, int(line[1]), int(line[2]))
                    #ZOMBIE
                     if int(line[3])== 2 :
                         ent = Zombie(0)
                         map.setEntity(ent, int(line[1]), int(line[2]))
                    #BERZERK
                     if int(line[3])== 3 :
                         ent = Berzerk()
                         map.setEntity(ent, int(line[1]), int(line[2]))
            
        
                elif line[0] == "shot" :
                    for i in range(rows+2):
                        for j in range(cols+2):
                            if map.getIDEntity(i,j) == line[1] :
                                map.getEntity(i,j).shotDone()
                    
       
                elif line[0] == "contagion" : 
                    for i in range(rows+2):
                        for j in range(cols+2):
                            if (map.getIDEntity(i,j) == int(line[1])) :
                                map.getEntity(i,j).contagionDone()
                                map.getEntity(int(float(line[3])), int(float(line[4]))).setId(int(float(line[2])) )
                
        
                elif line[0] == "zombie" :
                    for i in range(rows+2):
                        for j in range(cols+2):
                            if (map.getIDEntity(i,j) == line[1]) :
                                ent = map.getEntity(i,j)
                                map.removeEntity(i,j)
                                map.setEntity(ent, line[2], line[3])
                    
                elif line[0] == "dead" :
                    for i in range(rows+2):
                        for j in range(cols+2):
                            if (map.getIDEntity(i,j) == line[1]) :
                                map.removeEntity(i,j)
            
            for i in xrange (-1,rows+1) :
                for j in xrange (-1, cols+1) :
                    if (map.getSurface(i,j) == 'W') :
                        sys.stdout.write(map.getSurface(i,j))
                    else :
                        sys.stdout.write(map.getEntityString(i,j))
                        sys.stdout.flush()
                print
        
        comm.close()